package imoney.client.gwt;

import static imoney.client.gwt.GwtUtils.redirect;
import static imoney.shared.UrlUtils.createRedirURL;
import static imoney.shared.Statics.*;
import imoney.client.rpc.GreetingService;
import imoney.client.rpc.GreetingServiceAsync;
import imoney.shared.*;
import imoney.shared.exception.NoSuchPersonException;
import imoney.shared.exception.PositionOutOfDateException;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class IMoney implements EntryPoint {
	private FlexTable table;
	private VerticalPanel mainpanel = new VerticalPanel();
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		table = new FlexTable();
		Button testBouton = new Button("Utrecht Error Page");
		testBouton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				GwtUtils.redirect(DEFAULT_ERROR_URL);
			}
		});

		greet();

		mainpanel.add(table);
		mainpanel.add(testBouton);
		RootPanel.get("greet").add(mainpanel);
	}

	/**
	 * Method that ensure the cookie recording person index is present.
	 * This index is the position of the person in the list.
	 */
	private void greet() {
		String cookValue = Cookies.getCookie(Cookie_INDEX_OF_PERSON);
		if (cookValue == null) {
			// no cookie, the user has to be logged to the application.
			GwtUtils.redirect(UrlUtils.createRedirURL(LOGGIN_URL, Window.Location.getHref(), ""));
		}
		else {
			greetingService.greetServer(cookValue,new AsyncCallback<String>(){
				public void onFailure(Throwable caught) {
					if (caught instanceof PositionOutOfDateException) {
						// have to do it again because inside the callback
						String cookieValue = Cookies.getCookie(Cookie_INDEX_OF_PERSON);
						int position = ((PositionOutOfDateException) caught).getRealPosition();
						Cookies.setCookie(Cookie_INDEX_OF_PERSON, CookieBuilderShared.updatePosition(cookieValue, position));
					} else if (caught instanceof NoSuchPersonException) {
						Cookies.removeCookie(Cookie_INDEX_OF_PERSON);
						redirect(createRedirURL(LOGGIN_URL, Window.Location.getHref(), ""));						
					} else {
						caught.printStackTrace();
						redirect(DEFAULT_ERROR_URL);
					}
				}
				public void onSuccess(String result){
					Label nameLabel = new Label();
					nameLabel.setText("Bonjour "+result+" !");
					mainpanel.add(nameLabel);
				}
			});
		}
	}

}








